package com.rabbitmq.demo.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/rabbitmq")
public class RabbitMqController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    // 消息生产者
    @PostMapping("/postTest")
    public String sendMessageP(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageDate = "Test message,hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,String> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageDate);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("ordersExchange","roming.pass.report.test",map);
        return "success";
    }
    // 消息生产者
    @GetMapping("/getTest")
    public String sendMessageG(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageDate = "Test message,hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,String> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageDate);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("ordersExchange","roming.pass.report.test",map);
        return "success";
    }
}
